package com.css.bjdt_report.validation.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_report.cell.entity.Cell;
import com.css.bjdt_report.cell.service.CellService;
import com.css.bjdt_report.executive.report.ReportRequest;
import com.css.bjdt_report.report.entity.RptTemplateUnitRela;
import com.css.bjdt_report.report.service.ReportTemplateCommentService;
import com.css.bjdt_report.report.service.ReportTemplateUnitRelaService;
import com.css.bjdt_report.util.CommonUtil;
import com.css.bjdt_report.validation.entity.ValidationInfo;

@Service
public class ReportCommentValidateService {
	@Autowired
	private ReportTemplateUnitRelaService reportTemplateUnitRelaService;
	@Autowired
	private CellService cellService;
	@Autowired
	private ValidationInfoService validationInfoService;
	@Autowired
	private ReportTemplateCommentService reportTemplateCommentService;

	//----------------------------------------高杨（报表批注校核用的）--------------------------------------------------------
	/**
	 * 报表批注校核
	 * @param annualId
	 * @param templateId
	 * @param unitIds
	 */
	public void reportCommentValidation(String annualId, String templateId, String... unitIds) {
		for (String unitId : unitIds) {
			if (templateId == null) {
				insertReportCommentValidation(annualId, unitId);
			} else {
				insertReportCommentValidation(annualId, templateId, unitId);
			}

		}
	}

	//像批注校核里面插入数据(单张表的生成批注校核信息)
	public void insertReportCommentValidation(String annualId, String templateId, String unitId) {
		//首先查询出来报表模板中那些单元格是需要批注的
		List<Cell> cellList = cellService.getAllNeedCommentCell(templateId);
		ReportRequest rptBatchRequest = new ReportRequest();
		//根据年度、报表模板ID、单位ID查询出当前报表的实例ID
		List<RptTemplateUnitRela> viewRptTemplateUnitList = reportTemplateUnitRelaService
				.getTemplateUnitRela(rptBatchRequest);
		//得到报表实例ID
		String templateDataId = viewRptTemplateUnitList.get(0).getId();
		List<ValidationInfo> validationInfoList = new ArrayList<ValidationInfo>();
		for (Cell cell : cellList) {
			//查批注表里是否有记录
			String commentCount = reportTemplateCommentService.getReportCellCommentCount(templateDataId,
					cell.getAbsColNum(), cell.getAbsRowNum(), annualId);
			if (commentCount.equals("0")) {
				ValidationInfo validationInfo = new ValidationInfo();
				validationInfo.setId(CommonUtil.getUUID());
				validationInfo.setValidationType("5");
				validationInfo.setValidationState("2");
				validationInfo.setCellId(cell.getCellId());
				validationInfo.setActualExp(
						CommonUtil.numToLetter(cell.getRelColNum() + "") + cell.getRelRowNum() + "需要填写批注");
				validationInfoList.add(validationInfo);
			}
		}
		validationInfoService.batchInsertValidationInfo(annualId, validationInfoList);
	}

	//像批注校核里面插入数据(生成该单位的整个批注校核)
	public void insertReportCommentValidation(String annualId, String unitId) {
		//先根据单位ID和报表版本ID查询出所有的该单位下的需要填写的报表模板id
		ReportRequest rptBatchRequest = new ReportRequest();
		List<RptTemplateUnitRela> viewRptTemplateUnitList = reportTemplateUnitRelaService
				.getTemplateUnitRela(rptBatchRequest);
		List<ValidationInfo> validationInfoList = new ArrayList<ValidationInfo>();
		for (RptTemplateUnitRela viewRptTemplateUnit : viewRptTemplateUnitList) {
			//首先查询出来报表模板中那些单元格是需要批注的
			List<Cell> cellList = cellService.getAllNeedCommentCell(viewRptTemplateUnit.getTemplateId());
			for (Cell cell : cellList) {
				//查批注表里是否有记录
				String commentCount = reportTemplateCommentService.getReportCellCommentCount(
						viewRptTemplateUnit.getId(), cell.getAbsColNum(), cell.getAbsRowNum(), annualId);
				if (commentCount.equals("0")) {
					ValidationInfo validationInfo = new ValidationInfo();
					validationInfo.setId(CommonUtil.getUUID());
					validationInfo.setValidationType("5");
					validationInfo.setValidationState("2");
					validationInfo.setCellId(cell.getCellId());
					validationInfo.setActualExp(
							CommonUtil.numToLetter(cell.getRelColNum() + "") + cell.getRelRowNum() + "需要填写批注");
					validationInfoList.add(validationInfo);
				}
			}
		}
		validationInfoService.batchInsertValidationInfo(annualId, validationInfoList);
	}

	//----------------------------------------完--------------------------------------------------------

}
